const http = require('http')
const config=require('../config')

const Gamelist = async () => { //游戏列表
    const result = new Promise((resolve,reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:'/game/list/image/1?p=1&s=20',
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                let dataarr=JSON.parse(resdata).data.list
                dataarr.forEach(ele => {
                    if(ele.imageUrl[0].substr(0,1) == '/'){
                        ele.imageUrl[0]=ele.imageUrl[0].slice(1)
                    }
                })
                resolve(dataarr)
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    return result
}

const Gamerecommend = async () => { //游戏推荐
    const result = new Promise((resolve,reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:'/game/list/image/2?p=1&s=20',
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata).data.list)
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    return result
}

const pcGamerecommend = async () => { //PC端8个游戏推荐
    const result = new Promise((resolve,reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:'/game/list/image/2?p=1&s=8',
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata).data.list)
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    return result
}

const pcbaccaratv = async () => { //PC端真人百家乐
    const result = new Promise((resolve,reject) => {
        let opt={
            hostname:config.hostname,
            port: config.port,
            path:'/game/list/image/5?p=1&s=29',
            method: "GET"
        }
        let req=http.request(opt,(res) => {
            res.setEncoding('utf8')
            let resdata=''
            res.on('data',(chunk) => {
                resdata+=chunk
            })
            res.on('end',() => {
                resolve(JSON.parse(resdata).data.list)
            })
        })
        req.on('error',(e) => {
            reject(e)
        })
        req.end()
    })
    return result
}


module.exports={
    Gamelist,
    Gamerecommend,
    pcGamerecommend,
    pcbaccaratv
}